

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.*" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <center>
        <%
            String t=null;
            String d=null;
            String c;
             String itemName=null;
             int ci=0;
             //String ci=null;
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 if (!isMultipart) {
 } else {
   FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = null;
   try {
   items = upload.parseRequest(request);
   } catch (FileUploadException e) {
   out.println(e);
   }
   Iterator itr = items.iterator();
   while (itr.hasNext()) 
	   {
   FileItem item = (FileItem) itr.next();
   if (item.isFormField())
	   {
	      String name = item.getFieldName();
		  String value = item.getString();
		  if(name.equals("title"))
	           {
			   t=value;
			   }
			  if(name.equals("description"))
	                  {  
				         d=value;            		 
                         
					  }
                  if(name.equals("courseid"))
                          {
                            c=value;
                            }
} else
	   {
    try {
	
    itemName = item.getName();
   int index=itemName.lastIndexOf("\\");
   itemName=itemName.substring(index);
   out.println(getServletContext().getRealPath("/")+"material\\"+itemName);
   File savedFile = new File(getServletContext().getRealPath("/")+"material\\"+itemName);
   item.write(savedFile);
    }
    catch(Exception e)
    {
        out.println(e);
    }
    }
   }
   }
  
try{
  Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement st=con.createStatement();
String username=(String)session.getAttribute("username");
java.util.Date d1=new java.util.Date();
  String cd=d1.toString();
  String location="material/"+itemName;
  
  
  c=request.getParameter("courseid");
  //String t=request.getParameter("title");
 // String discription=request.getParameter("description");
  ci=Integer.parseInt(c);
  //ci=Integer.parseInt(c);
  st.executeUpdate("insert into course_material(courseid,title,description,location,creation_date)values ("+ci+",'"+t+"','"+d+"','"+location+"','"+cd+"')");
  request.setAttribute("msg", "material uploaded successfully");
   
        
          RequestDispatcher rd=request.getRequestDispatcher("/facultyuploadmaterial.jsp");
          rd.forward(request, response);
                   }
catch(Exception e)
               {
    e.printStackTrace();
}
  
%>
</center>
    </body>
</html>